package j05zuoye;

class WindowThread extends Thread{
    public WindowThread(String name) {
        super(name);
    }

    @Override
    public void run() {
        while (J2ticket.count>0){
            synchronized (J2ticket.lock){
                if(J2ticket.count<=0){
                    return;
                }
                J2ticket.count--;
                String name = Thread.currentThread().getName();
                System.out.println(name+"卖出一张票，当前剩余"+J2ticket.count+"张票");
            }
        }
    }

}

public class J2ticket {
    static int count = 1000;
    static Object lock = new Object();
    public static void main(String[] args) {
        WindowThread a = new WindowThread("窗口A");
        WindowThread b = new WindowThread("窗口B");
        WindowThread c = new WindowThread("窗口C");
        a.start();
        b.start();
        c.start();
    }
}


class MyRun implements Runnable{
    int count = 1000;
    @Override
    public void run() {
        while (count>0){
            sell();
        }
    }
    synchronized void sell(){
        if(count==0){
            return;
        }
        count--;
        String name = Thread.currentThread().getName();
        System.out.println(name+"卖出一张票，当前剩余"+count+"张票");
    }
}
class J3TicketRun{
    public static void main(String[] args) {
        MyRun r = new MyRun();
        Thread a = new Thread(r,"窗口A");
        Thread b = new Thread(r,"窗口B");
        Thread c = new Thread(r,"窗口C");
        a.start();
        b.start();
        c.start();
    }
}

